package com.zbmsg.message.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zbmsg.group.bean.Group;
import com.zbmsg.group.service.IGroupService;
import com.zbmsg.message.service.MessageService;
import com.zbmsg.server.Global;
import com.zbmsg.user.bean.User;
import com.zbmsg.user.service.IUserService;
import com.zbmsg.utils.common.MessageResult;
import com.zbmsg.utils.common.Request;
import com.zbmsg.utils.common.bean.GroupBean;
import com.zbmsg.utils.common.bean.MsgBean;
import com.zbmsg.utils.common.enums.MessageType;
import com.zbmsg.utils.time.TimeUtils;

import io.netty.channel.Channel;

@Service("messageService")
public class MessageServiceImpl implements MessageService {

	@Autowired
	private IUserService userService;
	@Autowired
	private IGroupService groupService;

	@Override
	public MessageResult messageHandle(Request req, Channel channel) throws Exception {
		MessageResult msgR = new MessageResult();
		if (req.getType() == MessageType.CREATEGROUP.toInt()) {
			GroupBean groupBean = req.toObject(GroupBean.class);
			User user = this.userService.setChannel(groupBean.getUserId(), channel);
			if (user == null) {
				msgR.setMessageType(MessageType.CREATEGROUP.toInt());
				msgR.setMsg("账户不存在");
				msgR.setTime(TimeUtils.getCurrentTime());
				return msgR;
			}
			Group group = this.groupService.isCreateGroup(groupBean.getUserId());
			Global.channelGroups.get(groupBean.getGroupId()).add(channel);
			if(group != null) {
				msgR.setMessageType(MessageType.CREATEGROUP.toInt());
				msgR.setMsg(user.getName() + "加入了房间");
				msgR.setTime(TimeUtils.getCurrentTime());
				msgR.setObj(user);
				return msgR;
			}
			user.setGroupId(groupBean.getGroupId());
			this.userService.updateUser(user);
			msgR.setMessageType(MessageType.CREATEGROUP.toInt());
			msgR.setMsg(user.getName() + "创建了房间");
			msgR.setObj(user);
			msgR.setTime(TimeUtils.getCurrentTime());
		}
		if(req.getType() == MessageType.JOINGROUP.toInt()) {
			GroupBean groupBean = req.toObject(GroupBean.class);
			User user = this.userService.setChannel(groupBean.getUserId(), channel);
			if (user == null) {
				msgR.setMessageType(MessageType.JOINGROUP.toInt());
				msgR.setMsg("账户不存在");
				msgR.setTime(TimeUtils.getCurrentTime());
				return msgR;
			}
			Global.channelGroups.get(groupBean.getGroupId()).add(channel);
			user.setGroupId(groupBean.getGroupId());
			this.userService.updateUser(user);
			msgR.setMessageType(MessageType.JOINGROUP.toInt());
			msgR.setMsg(user.getName() + "加入了房间");
			msgR.setTime(TimeUtils.getCurrentTime());
			msgR.setObj(user);
		}
		if(req.getType() == MessageType.SENDMSG.toInt()) {
			//此处可优化为  利用channel来寻找账户,根据账户的房间id来进行群发,无需页面传参
			MsgBean msgBean = req.toObject(MsgBean.class);
			User user = this.userService.getUserByUserId(msgBean.getUserId());
			if (user == null) {
				msgR.setMessageType(MessageType.SENDMSG.toInt());
				msgR.setMsg("账户不存在");
				msgR.setTime(TimeUtils.getCurrentTime());
				return msgR;
			}
			msgR.setMessageType(MessageType.RECEIVEMSG.toInt());
			msgR.setMsg(user.getName() + ":"+msgBean.getMsg());
			msgR.setTime(TimeUtils.getCurrentTime());
			msgR.setObj(user);
		}
		return msgR;
	}

}
